package com.projectpractice.controller;

import com.projectpractice.bean.Article;
import com.projectpractice.bean.Classify;
import com.projectpractice.bean.User;
import com.projectpractice.common.utils.Result;
import com.projectpractice.common.utils.ResultUtils;
import com.projectpractice.service.ArticleService;
import com.projectpractice.service.ClassifyService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @Autowired
    ClassifyService classifyService;

    @RequestMapping("/getArticle")
    public Result getArticle(){
        List<Article> list=articleService.getArticles();
        Result result= ResultUtils.success(list);
        result.setCode(0);
        result.setMsg("查询成功");
        return result;
    }

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(){
        ModelAndView mv=new ModelAndView();
        User user=(User)SecurityUtils.getSubject().getPrincipal();
        mv.addObject("user", user);
        mv.setViewName("article_add");
        return mv;
    }

    @RequestMapping("/doAdd")
    public Result doAdd(Article article){
        article.setAstate(1);
        System.out.println(article);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        article.setCid(article.getCid());
        article.setAcreatetime(dateFormat.format(new Date()));
        Article registArticle=articleService.regist(article);
        System.out.println("添加数据["+article+"]");
        Result result=ResultUtils.success(registArticle);
        result.setMsg("添加成功");
        return result;
    }
    @RequestMapping("/toAddClassify")
    public ModelAndView toAddClassify(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("classify_add");
        return mv;
    }

    @RequestMapping("/doAddClassify")
    public Result doAddClassify(Classify classify){
        Classify registClassify=classifyService.addClassify(classify);
        System.out.println("添加数据["+classify+"]");
        Result result=ResultUtils.success(registClassify);
        result.setMsg("添加成功");
        return result;
    }

    @RequestMapping("/list")
    public ModelAndView toList(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("article_list");
        return mv;
    }

    @RequestMapping("/delete/{aid}")
    public Result delete(@PathVariable Integer aid){
        Result result=null;
        int data=articleService.delete(aid);
        result=ResultUtils.success(data);
        if(data>0){
            result.setCode(0);
            result.setMsg("删除成功");
        }
        else{
            result.setMsg("删除失败");
        }
        return result;
    }

    @RequestMapping("/deleteClassify/{cid}")
    public Result deleteClassify(@PathVariable Integer cid){
        Result result=null;
        int data=classifyService.delete(cid);
        result=ResultUtils.success(data);
        if(data>0){
            result.setCode(0);
            result.setMsg("删除成功");
        }
        else{
            result.setMsg("删除失败");
        }
        return result;
    }

    @RequestMapping("/toEdit/{aid}")
    public ModelAndView toEdit(@PathVariable Integer aid){
        ModelAndView mv=new ModelAndView();
        //设置视图
        mv.setViewName("article_edit");
        Article article=articleService.getArticleById(aid);
        //设置数据
        mv.addObject("article",article);
        return mv;
    }

    @RequestMapping("/edit")
    public Result edit(Article article){
        Result result=null;
        // 先获取原来对象的值
        System.out.println(article);
        Article a=articleService.getArticleById(article.getAid());
        a.setAstate(1);
        a.setCid(article.getCid());
        a.setTitle(article.getTitle());
        a.setScontent(article.getScontent());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        a.setAcreatetime(dateFormat.format(new Date()));
        int data=articleService.update(a);
        result=ResultUtils.success(data);
        if(data>0){
            result.setCode(0);
            result.setMsg("更新成功");
        }
        else{
            result.setMsg("更新失败");
        }
        return result;
    }

    @RequestMapping("/toDetail/{aid}")
    public ModelAndView toDetail(@PathVariable Integer aid){
        ModelAndView mv=new ModelAndView();
        //设置视图
        Classify classify=classifyService.getClassifyByCid(articleService.getArticleById(aid).getCid());
        mv.addObject("classify",classify);
        mv.setViewName("article_detail");
        Article article=articleService.getArticleById(aid);
        //设置数据
        mv.addObject("article",article);

        return mv;
    }

    @RequestMapping("/state")
    public Result state(Article article){
        Result result=null;
        // 先获取原来对象的值
        Article a=articleService.getArticleById(article.getAid());
        a.setAstate(article.getAstate());
        System.out.println(a);
        int data=articleService.update(a);
        result=ResultUtils.success(data);
        if(data>0){
            result.setCode(0);
            result.setMsg("更新成功");
        }
        else{
            result.setMsg("更新失败");
        }
        return result;
    }
    @RequestMapping("/classify_list")
    public Result allClassify() {
        List<Classify> list=classifyService.getClassify();
        Result result= ResultUtils.success(list);
        result.setCode(0);
        result.setMsg("查询成功");
        return result;
    }

    @RequestMapping("/classifylist")
    public ModelAndView classifyList(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("classify_list");
        return mv;
    }
}
